package com.ruoyi.inspection.domain.receiveVo;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.constant.GlobalConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.inspection.config.JobBillTypeEnum;
import com.ruoyi.inspection.config.JobDepartmentEnum;
import com.ruoyi.inspection.constant.InspectionConstant;
import com.ruoyi.system.domain.JobPlanBillDeclare;
import lombok.Data;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 上报 特殊作业活动信息
 */
@Data
public class ReceiveJobPlan implements Serializable {
    //// 唯一编码，36 位uuid，必填
    private String id;
    //企业编码
    private String companyCode;
    //作业类型
    private String ticketType;
    // 作业开始时间
    private String startTime;
    // 结束时间
    private String endTime;
    //作业地点
    private String ticketPosition;
    //作业内容
    private String jobContent;
    //安全监管人员姓名（监管人员姓名，通过英文逗号隔开）
    private String supervisorName;
    //施工人员 (通过英文逗号隔开)
    private String workerName;
    ///施工单位名称
    private String workDepartment;
    // 施工人员编码：人员基础信息的人员id，多个通过英文逗号隔开
    private String personId;
    // 施工单位统一社会信用代码，当是承包商作业时，需上传，多个通过英文逗号隔开
    private String socialCreditCode;
    //是否承包商作业（0、否 1、是）
    private String isContractorWork;
    //作业负责
    private String ticketResponsName;
    ////负责人联系方式，
    private String majorPersonMobile;
    // 删除状态 0有效 1删除
    private String deleted;
    private String createBy;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private String createDate;
    private String updateBy;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private String updateDate;


    /**
     * 构建作业票对象
     * @return
     */
    public static ReceiveJobPlan jobPlanBillToReceiveJobPlan(JobPlanBillDeclare jobPlan) {
        String governId = jobPlan.getGovernId();
        governId = StringUtils.isEmpty(governId) ? UUID.randomUUID().toString() : governId;
        // 作业票起始、截至时间
        String startDate = jobPlan.getJobPlanStartDate();
        String endDate = jobPlan.getJobPlanEndDate();
        String startTime = jobPlan.getJobPlanStartTime();
        String endTime = jobPlan.getJobPlanEndTime();
        startTime = StringUtils.isNotEmpty(startTime)
                ? startDate + " " + startTime + ":00"
                : startDate + " 00:00:00";
        endTime = StringUtils.isNotEmpty(endTime)
                ? endDate + " " + endTime + ":00"
                : endDate + " 23:59:59";

        // 作业内容
        String zynr = jobPlan.getJobContent();
        String local = jobPlan.getLocal();
        // 作业类型
        String jobType = jobPlan.getJobType();
        if (StringUtils.isNotEmpty(jobType)) {
            jobType = Arrays.stream(jobType.split(","))
                    .map(typeName -> JobBillTypeEnum.valueOfCode(typeName))
                    .filter(StringUtils::isNotEmpty)
                    .collect(Collectors.joining(","));
        }
        // 作业负责人
        String zyfzr = jobPlan.getJobPersonCharge();
        // 负责人联系方式
        String phone = jobPlan.getPhone();
        Date createDate = jobPlan.getCreatedDate();
        Date updateDate = jobPlan.getModifyDate()==null ? createDate : jobPlan.getModifyDate();
        String updateDateStr = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", updateDate);
        // 是否承包商作业
        String isContractorWork = jobPlan.getContractorJob();
        // 作业单位
        String zydw = jobPlan.getWorkShop();
        // 作业人
        String zyr = "";
        String zyrIds = "";
        String jobPersonnel = jobPlan.getJobPersonnel();
        JSONArray jsonArray = JSONUtil.parseArray(jobPersonnel);
        if (jsonArray!=null && !jsonArray.isEmpty()) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject object = jsonArray.getJSONObject(i);
                zyr += object.getStr("name") + ",";
                zyrIds += object.getInt("id") + ",";
            }
            zyrIds = StringUtils.isNotEmpty(zyrIds) ? zyrIds.substring(0, zyrIds.length() - 1) : zyrIds;
            zyr = StringUtils.isNotEmpty(zyr) ? zyr.substring(0, zyr.length() - 1) : zyr;
        }
        // 社会信用代码
        String department = jobPlan.getWorkShop();
        String socialCreditCode = JobDepartmentEnum.valueOfCode(department);


        ReceiveJobPlan jobPlanVo = new ReceiveJobPlan();
        jobPlanVo.setId(governId);
        jobPlanVo.setCompanyCode(GlobalConstants.COMPANY_CODE);
        jobPlanVo.setTicketType(jobType);
        jobPlanVo.setStartTime(startTime);
        jobPlanVo.setEndTime(endTime);
        jobPlanVo.setTicketPosition(local);
        jobPlanVo.setJobContent(zynr);
        jobPlanVo.setSupervisorName(zyfzr);
        jobPlanVo.setWorkerName(zyr);
        jobPlanVo.setWorkDepartment(zydw);
        jobPlanVo.setPersonId(zyrIds);
        jobPlanVo.setSocialCreditCode(socialCreditCode);
        jobPlanVo.setIsContractorWork(isContractorWork);
        jobPlanVo.setTicketResponsName(zyfzr);
        jobPlanVo.setMajorPersonMobile(phone);
        jobPlanVo.setDeleted("0");
        jobPlanVo.setCreateDate(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", jobPlan.getCreatedDate()));
        jobPlanVo.setCreateBy(StringUtils.isEmpty(jobPlan.getCreatedBy()) ? "admin" : jobPlan.getCreatedBy());
        jobPlanVo.setUpdateDate(updateDateStr);
        jobPlanVo.setUpdateBy(jobPlan.getModifyBy()==null ? "admin" : jobPlan.getModifyBy());

        jobPlan.setGovernId(governId);
        return jobPlanVo;
    }


}
